import 'muse-ui/dist/muse-ui.css'
import 'babel-polyfill'
import Vue from 'vue'
import App from './App'
import store from './vuex/store'
import MuseUI from 'muse-ui'
import * as filters from '@/common/js/filters'
import { GetRequest } from './common/js/util'
import VueWechatTitle from 'vue-wechat-title'

// Add lazyload directive
import VueLazyload from 'vue-lazyload'
import errorImg from './assets/images/head_images2.png'

import { APP_PLATFORM } from '@/api/api'
import Datepicker from 'vuejs-datepicker-mobile';
Vue.use(Datepicker);

import router from './router'
import { doFormPostRequest, commonAjaxJson } from '@/api/api';
import api from '@/api/interface';

Vue.config.devtools = true

Vue.prototype.$api = { doFormPostRequest, api };
Vue.prototype.$API = api
Vue.prototype.$postJson = commonAjaxJson

Vue.use(VueWechatTitle)
// register filters
for (var f in filters) {
  Vue.filter(f, filters[f])
}

MuseUI.config({
  disableTouchRipple: false,
  disableFocusRipple: true
})
Vue.use(MuseUI)
Vue.use(VueLazyload, {
  error: errorImg,
  loading: errorImg
})
/**
 * Mock server for devolopment
 * import Mock from './mock'
 * Mock.start()
*/
/**
 * 环境适配
 * 支持微信公众号和支付宝生活号
 */
const APP_PLATFORM_NAME = ['', 'openId', 'aliUid'][APP_PLATFORM]
Vue.prototype.$APP_PLATFORM = APP_PLATFORM
Vue.prototype.$APP_PLATFORM_NAME = APP_PLATFORM_NAME
Vue.prototype.$log = (msg) => console.log(msg)
/**
 * 导航钩子
 */

router.beforeEach((to, from, next) => {
  if (to.meta.title) {
    document.title = to.meta.title
  }
  const querystring = GetRequest(to.fullPath)
  if (querystring.token) {
    sessionStorage.setItem('token', querystring.token)
  }
  if (querystring.familyPerson && querystring.familyPerson !== "") {
    let fp = querystring.familyPerson
    sessionStorage.setItem('familyPerson', fp)
  }
  if (querystring.sn) {
    sessionStorage.setItem('sn', querystring.sn)
  }
  if (querystring.userId) {
    sessionStorage.setItem('userId', querystring.userId)
  }
  if (querystring.device) {
    sessionStorage.setItem('device', querystring.device)
  }
  if (querystring.roleType) {
    sessionStorage.setItem('roleType', querystring.roleType)
  }
  if (querystring.hospitalCode) {
    sessionStorage.setItem('hospitalCode', querystring.hospitalCode)
  }
  let query = {
    redirect: to.fullPath,
    [APP_PLATFORM_NAME]: sessionStorage[APP_PLATFORM_NAME]
  }
  if (to.matched.some(record => record.meta.requiresAuth)) {
    if (sessionStorage.token) {
      next()
    } else {
      next({
        path: '/login',
        query: query
      })
    }
  } else {
    next()
  }
})

Vue.config.productionTip = false
/* eslint-disable no-new */
new Vue({
  router,
  store,
  render: h => h(App),
  mounted() {
    document.dispatchEvent(new Event('render-event'))
  }
}).$mount('#app')
